/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.portlet.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import com.portlet.service.ClpSerializer;
import com.portlet.service.roleLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Thi
 */
public class roleClp extends BaseModelImpl<role> implements role {
	public roleClp() {
	}

	public Class<?> getModelClass() {
		return role.class;
	}

	public String getModelClassName() {
		return role.class.getName();
	}

	public String getPrimaryKey() {
		return _maRole;
	}

	public void setPrimaryKey(String primaryKey) {
		setMaRole(primaryKey);
	}

	public Serializable getPrimaryKeyObj() {
		return _maRole;
	}

	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey((String)primaryKeyObj);
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("maRole", getMaRole());
		attributes.put("tenRole", getTenRole());
		attributes.put("ghiChu", getGhiChu());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		String maRole = (String)attributes.get("maRole");

		if (maRole != null) {
			setMaRole(maRole);
		}

		String tenRole = (String)attributes.get("tenRole");

		if (tenRole != null) {
			setTenRole(tenRole);
		}

		String ghiChu = (String)attributes.get("ghiChu");

		if (ghiChu != null) {
			setGhiChu(ghiChu);
		}
	}

	public String getMaRole() {
		return _maRole;
	}

	public void setMaRole(String maRole) {
		_maRole = maRole;

		if (_roleRemoteModel != null) {
			try {
				Class<?> clazz = _roleRemoteModel.getClass();

				Method method = clazz.getMethod("setMaRole", String.class);

				method.invoke(_roleRemoteModel, maRole);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public String getTenRole() {
		return _tenRole;
	}

	public void setTenRole(String tenRole) {
		_tenRole = tenRole;

		if (_roleRemoteModel != null) {
			try {
				Class<?> clazz = _roleRemoteModel.getClass();

				Method method = clazz.getMethod("setTenRole", String.class);

				method.invoke(_roleRemoteModel, tenRole);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public String getGhiChu() {
		return _ghiChu;
	}

	public void setGhiChu(String ghiChu) {
		_ghiChu = ghiChu;

		if (_roleRemoteModel != null) {
			try {
				Class<?> clazz = _roleRemoteModel.getClass();

				Method method = clazz.getMethod("setGhiChu", String.class);

				method.invoke(_roleRemoteModel, ghiChu);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getroleRemoteModel() {
		return _roleRemoteModel;
	}

	public void setroleRemoteModel(BaseModel<?> roleRemoteModel) {
		_roleRemoteModel = roleRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _roleRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_roleRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	public void persist() throws SystemException {
		if (this.isNew()) {
			roleLocalServiceUtil.addrole(this);
		}
		else {
			roleLocalServiceUtil.updaterole(this);
		}
	}

	@Override
	public role toEscapedModel() {
		return (role)ProxyUtil.newProxyInstance(role.class.getClassLoader(),
			new Class[] { role.class }, new AutoEscapeBeanHandler(this));
	}

	public role toUnescapedModel() {
		return this;
	}

	@Override
	public Object clone() {
		roleClp clone = new roleClp();

		clone.setMaRole(getMaRole());
		clone.setTenRole(getTenRole());
		clone.setGhiChu(getGhiChu());

		return clone;
	}

	public int compareTo(role role) {
		String primaryKey = role.getPrimaryKey();

		return getPrimaryKey().compareTo(primaryKey);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof roleClp)) {
			return false;
		}

		roleClp role = (roleClp)obj;

		String primaryKey = role.getPrimaryKey();

		if (getPrimaryKey().equals(primaryKey)) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return getPrimaryKey().hashCode();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(7);

		sb.append("{maRole=");
		sb.append(getMaRole());
		sb.append(", tenRole=");
		sb.append(getTenRole());
		sb.append(", ghiChu=");
		sb.append(getGhiChu());
		sb.append("}");

		return sb.toString();
	}

	public String toXmlString() {
		StringBundler sb = new StringBundler(13);

		sb.append("<model><model-name>");
		sb.append("com.portlet.model.role");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>maRole</column-name><column-value><![CDATA[");
		sb.append(getMaRole());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>tenRole</column-name><column-value><![CDATA[");
		sb.append(getTenRole());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>ghiChu</column-name><column-value><![CDATA[");
		sb.append(getGhiChu());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private String _maRole;
	private String _tenRole;
	private String _ghiChu;
	private BaseModel<?> _roleRemoteModel;
}